home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / QuickTime / JPEG Sample / Source / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1996-05-22  |  11.0 KB  |  471 lines  |  [TEXT/MPCC]

  1. /*************************************************************************************
  2. #
  3. #        Windows.c
  4. #
  5. #        This segment handles the window creation, close, updates,
  6. #
  7. #        Author(s):     Michael Marinkovich & Guillermo Ortiz
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            4/3/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Windows.h>
  29.  
  30. #include "App.h"
  31. #include "Proto.h"
  32.  
  33.  
  34. //----------------------------------------------------------------------
  35. //
  36. //    Globals - 
  37. //
  38. //----------------------------------------------------------------------
  39.  
  40. extern Boolean        gHasAbout;        // have an about box?
  41. extern short        gWindCount;
  42.  
  43.  
  44. //----------------------------------------------------------------------
  45. //
  46. //    CreateWindow - create a window from the info passed in. Will try to 
  47. //                   load from resource if resID is supplied.
  48. //
  49. //----------------------------------------------------------------------
  50.  
  51. WindowPtr CreateWindow(short resID, void *wStorage, Rect *bounds,  Str255 title,
  52.                         Boolean visible, short procID , short kind,
  53.                         WindowRef behind, Boolean goAwayFlag, long refCon)
  54. {
  55.     OSErr            err = nil;
  56.     WindowRef        newWindow = nil;
  57.     
  58.     
  59.     if (resID != nil)         // if res id isn't nil then load from disk
  60.         newWindow = GetNewWindow(resID, wStorage, behind);
  61.     else                    // otherwise make a new windowRecord
  62.          newWindow = NewWindow(wStorage, bounds, title, visible, 
  63.                                procID, behind, goAwayFlag, refCon);
  64.                                
  65.     if (newWindow != nil) {
  66.         NewWindowTitle(newWindow, title);
  67.         err = InitWindowProcs(newWindow, kind);
  68.         
  69.         if (err == noErr) {
  70.             SetPort(newWindow);
  71.             ShowWindow(newWindow);
  72.         }
  73.                 
  74.         // initialization of Document Record faild
  75.         // so kill the return window    
  76.         else {
  77.             newWindow = nil;
  78.             HandleError(err, false);
  79.         }        
  80.     }
  81.     
  82.     return newWindow;
  83.  
  84. }
  85.     
  86.  
  87. //----------------------------------------------------------------------
  88. //
  89. //    RemoveWindow - applications Doc window disposal routine.
  90. //                 
  91. //
  92. //----------------------------------------------------------------------
  93.  
  94. OSErr RemoveWindow( WindowRef window )
  95. {
  96.     OSErr            err = nil;
  97.     short            kind;
  98.     DocHnd            doc;
  99.     
  100.     
  101.     kind = GetWindKind( window );
  102.     if ( kind < kDocKind || kind > kAboutKind )
  103.         return -1; // not our window
  104.     
  105.     doc = (DocHnd)GetWRefCon( window );
  106.     if ( doc != nil ) {
  107.         switch( kind ) {
  108.             case kDocKind:
  109.                 DisposeGWorld((**doc).world);
  110.                 DisposeHandle( (Handle)doc );
  111.                 DisposeWindow( window );
  112.                 err = noErr;
  113.                 break;
  114.             
  115.             case kAboutKind:
  116.                 DisposeHandle( (Handle)doc );
  117.                 DisposeWindow( window );
  118.                 err = noErr;
  119.                 gHasAbout = false;    // allow new about box
  120.                 break;
  121.             
  122.             default:
  123.                 break;
  124.                     
  125.  
  126.         }    
  127.     }
  128.     
  129.     return err;
  130. }        
  131.  
  132.  
  133. //----------------------------------------------------------------------
  134. //
  135. //    NewWindowTitle - if supplied title is nil then title is set to 
  136. //                      global "gWindCount".
  137. //
  138. //----------------------------------------------------------------------
  139.  
  140. void NewWindowTitle(WindowRef window, Str255 str)
  141. {
  142.     Str255        catStr = "\pUntitled ";
  143.     Str255        newStr;
  144.     
  145.     
  146.     if (str == nil || StrLength(str) == 0) {
  147.         pstrcpy(newStr, catStr);
  148.         NumToString(gWindCount, catStr);
  149.         pstrcat(newStr, catStr);
  150.         gWindCount++;
  151.         
  152.         SetWTitle(window,newStr);
  153.     }
  154.     else
  155.         SetWTitle(window, str);
  156.  
  157. }
  158.  
  159.  
  160. //----------------------------------------------------------------------
  161. //
  162. //    InitWindowProcs - init a window with proper callback event. fills 
  163. //                       out custom procs for different windowkinds.
  164. //
  165. //----------------------------------------------------------------------
  166.  
  167. OSErr InitWindowProcs(WindowRef window, short windKind)
  168. {
  169.     OSErr            err = nil;
  170.     DocHnd            doc;
  171.     
  172.     doc = (DocHnd)NewHandle(sizeof(DocRec));
  173.     if (doc != nil) {
  174.         SetWRefCon(window, (long)doc);
  175.  
  176.         switch(windKind) {
  177.             case kDocKind:
  178.                 (**doc).idleProc        = DoIdle;
  179.                 (**doc).mMenuProc        = HandleMenuChoice;
  180.                 (**doc).inContentProc    = HandleContentClick;
  181.                 (**doc).inGoAwayProc    = nil;
  182.                 (**doc).inZoomProc        = HandleZoomClick;
  183.                 (**doc).inGrowProc        = HandleGrow;
  184.                 (**doc).keyProc            = nil;
  185.                 (**doc).activateProc    = DoActivate;
  186.                 (**doc).updateProc        = DrawWindow;    
  187.                 (**doc).hScroll         = nil;
  188.                 (**doc).vScroll            = nil;
  189.                 (**doc).world            = nil;
  190.                 (**doc).pict            = nil;
  191.                 (**doc).printer            = nil;
  192.                 (**doc).dirty            = false;
  193.                 
  194.                 InstallScrollBars(window, doc);
  195.  
  196.                 break;
  197.                 
  198.             case kDialogKind:
  199.                 break;
  200.  
  201.             case kAboutKind:
  202.                 (**doc).idleProc        = DoIdle;
  203.                 (**doc).mMenuProc        = HandleMenuChoice;
  204.                 (**doc).inContentProc    = nil;
  205.                 (**doc).inGoAwayProc    = nil;
  206.                 (**doc).inZoomProc        = nil;
  207.                 (**doc).inGrowProc        = nil;
  208.                 (**doc).keyProc            = nil;
  209.                 (**doc).activateProc    = nil;
  210.                 (**doc).updateProc        = DrawAboutWindow;    
  211.                 (**doc).hScroll         = nil;
  212.                 (**doc).vScroll            = nil;
  213.                 (**doc).world            = nil;
  214.                 (**doc).pict            = GetPicture(rAboutPictID);
  215.                 (**doc).printer            = nil;
  216.                 (**doc).dirty            = false;
  217.                 
  218.                 break;
  219.                 
  220.             default:
  221.                 err = 25;
  222.                 break;    
  223.         }
  224.         ((WindowPeek)window)->windowKind = windKind;
  225.  
  226.     }            
  227.     return err;
  228.  
  229. }
  230.  
  231.  
  232. //----------------------------------------------------------------------
  233. //
  234. //    PictToWorld - create a GWorld from a Pict. Size is determined by the
  235. //                  bounds of the pict. If the pict is nil then a default
  236. //                  bounds is used, in case of an empty window.
  237. //----------------------------------------------------------------------
  238.     
  239. GWorldPtr PictToWorld(PicHandle pict, OSErr *rtnErr)
  240. {
  241.     OSErr            err;
  242.     GWorldPtr        oldWorld;
  243.     GWorldPtr        theWorld = nil;
  244.     GDHandle        oldGD;
  245.     PixMapHandle    thePix;
  246.     Rect            bounds;
  247.     
  248.     GetGWorld(&oldWorld, &oldGD);
  249.     
  250.     if (pict != nil)
  251.         bounds = (**pict).picFrame;
  252.     else
  253.         SetRect(&bounds, 0, 0, 200, 200);    
  254.     
  255.     err = NewGWorld(&theWorld, 8,&bounds, nil, nil, 0L);
  256.     if (err == noErr && theWorld != nil) {
  257.         thePix = GetGWorldPixMap(theWorld);
  258.         if (LockPixels(thePix)) {
  259.             SetGWorld(theWorld, nil);
  260.             EraseRect(&bounds);
  261.             if (pict != nil)
  262.                 DrawPicture(pict, &bounds);
  263.             
  264.             UnlockPixels(thePix);
  265.         }
  266.         SetGWorld(oldWorld, oldGD);
  267.     }
  268.     
  269.     *rtnErr = err;
  270.     
  271.     return theWorld;
  272. }
  273.  
  274.         
  275. //----------------------------------------------------------------------
  276. //
  277. //    DrawWindow - custom proc that is called to update window contents.
  278. //                 
  279. //
  280. //----------------------------------------------------------------------
  281.  
  282. void DrawWindow(WindowRef window, void *refCon)
  283. {
  284.     DocHnd            doc;
  285.     GWorldPtr        theWorld;
  286.     PixMapHandle    thePix;
  287.     Rect            cRect;
  288.     Rect            bounds;
  289.     
  290.     doc = (DocHnd)GetWRefCon(window);    
  291.     if (doc != nil) {
  292.         SetPort(window);
  293.         GetContRect(window, &cRect);
  294.         ClipRect(&cRect);
  295.         
  296.         theWorld = (**doc).world;
  297.  
  298.         if (theWorld != nil ) {
  299.             bounds = theWorld->portRect;
  300.             OffsetRect(&bounds, -GetCtlValue((**doc).hScroll), 
  301.                        -GetCtlValue((**doc).vScroll));
  302.             thePix = GetGWorldPixMap(theWorld);
  303.             if (LockPixels(thePix)) {
  304.                 CopyBits((BitMap *) *thePix, &window->portBits,
  305.                          &theWorld->portRect, &bounds, srcCopy, nil);
  306.                 
  307.                 UnlockPixels(thePix);
  308.             }             
  309.                         
  310.         }
  311.         else
  312.             EraseRect(&cRect);    
  313.  
  314.         ClipRect(&window->portRect);
  315.         DrawGrowIcon(window);
  316.         UpdateControls(window, window->visRgn);
  317.     }
  318.  
  319. }
  320.  
  321.  
  322. //----------------------------------------------------------------------
  323. //
  324. //    DrawAboutWindow - custom proc that is called to update about window.
  325. //                 
  326. //
  327. //----------------------------------------------------------------------
  328.  
  329. void DrawAboutWindow( WindowRef window, void *refCon )
  330. {    
  331.     DocHnd        doc;
  332.     
  333.     doc = (DocHnd)GetWRefCon(window);    
  334.     if ( (**doc).pict != nil ) 
  335.         DrawPicture( (**doc).pict, &window->portRect );
  336.  
  337. }
  338.  
  339.  
  340. //----------------------------------------------------------------------
  341. //
  342. //    DoResizeWindow - custom proc that is called to update window.
  343. //                 
  344. //
  345. //----------------------------------------------------------------------
  346.  
  347. void DoResizeWindow (WindowRef window) 
  348. {
  349.     DocHnd            doc;
  350.     ControlHandle    hCtl, vCtl;
  351.     RgnHandle        tempRgn;
  352.     RgnHandle        oldCtlRgn;
  353.     short            max, oldCntlVal;
  354.     short            pV, wV;
  355.     short             pH, wH;
  356.     Rect            hSRect;
  357.     Rect            newDocRect;
  358.     Rect            pictRect;
  359.     Rect            paneRect;
  360.  
  361.     doc = (DocHnd)GetWRefCon(window);
  362.     if (doc != nil) {
  363.         pictRect = (**doc).world->portRect;
  364.         newDocRect = (**window->visRgn).rgnBBox;
  365.  
  366.         hCtl = (**doc).hScroll;
  367.         vCtl = (**doc).vScroll;
  368.         
  369.         hSRect = (**hCtl).contrlRect;
  370.         hSRect.right += kScrollWidth;
  371.         ClipRect(&window->portRect);
  372.         RectRgn(oldCtlRgn = NewRgn(), &hSRect);
  373.         RectRgn(tempRgn = NewRgn(), &(**vCtl).contrlRect);
  374.         UnionRgn(oldCtlRgn, tempRgn, oldCtlRgn);
  375.         EraseRgn(oldCtlRgn);
  376.         
  377.         (**hCtl).contrlVis = 0;
  378.         (**vCtl).contrlVis = 0;
  379.  
  380.         paneRect = newDocRect;
  381.         paneRect.bottom -= kScrollWidth;
  382.         paneRect.right -= kScrollWidth;
  383.         
  384.         MoveControl(hCtl, paneRect.left - 1, paneRect.bottom);    
  385.         MoveControl(vCtl, paneRect.right, paneRect.top - 1);
  386.             
  387.         SizeControl(hCtl, 2+paneRect.right - paneRect.left, kScrollWidth + 1);    
  388.         SizeControl(vCtl, kScrollWidth + 1, 2+paneRect.bottom - paneRect.top);
  389.         
  390.         (**hCtl).contrlVis = 255;
  391.         (**vCtl).contrlVis = 255;
  392.         
  393.         pV = pictRect.bottom - pictRect.top;
  394.         wV = paneRect.bottom - paneRect.top;
  395.         if (wV >= pV)
  396.             max = 0;
  397.         else
  398.             max = ((MAX (0, pV - wV)) / kScrollDelta) + 1;
  399.             
  400.         oldCntlVal = GetControlValue(vCtl);
  401.         SetControlMinimum(vCtl, 0);
  402.         SetControlMaximum(vCtl, max);
  403.         SetControlValue(vCtl, oldCntlVal );
  404.         
  405.         HiliteControl(vCtl, (max == 0) ? (255):(0));    
  406.  
  407.         pH = pictRect.right - pictRect.left;
  408.         wH = paneRect.right - paneRect.left;
  409.         if (wH >= pH)
  410.             max = 0 ;
  411.         else
  412.             max = ((MAX (0, pH - wH )) / kScrollDelta) +1;
  413.         oldCntlVal = GetControlValue(hCtl);
  414.         SetControlMinimum(hCtl, 0);
  415.         SetControlMaximum(hCtl, max) ;
  416.         SetControlValue(hCtl, oldCntlVal);
  417.         
  418.         HiliteControl(hCtl, (max == 0) ? (255):(0));    
  419.                 
  420.         DisposeRgn(oldCtlRgn);
  421.         DisposeRgn(tempRgn);
  422.         AdjustScrollValues(window);
  423.         InvalRect(&window->portRect);
  424.  
  425.     }
  426.  
  427. }
  428.  
  429.  
  430. //----------------------------------------------------------------------
  431. //
  432. //    GetWindKind - returns the windowkind.
  433. //                 
  434. //
  435. //----------------------------------------------------------------------
  436.  
  437. short GetWindKind(WindowRef window)
  438. {
  439.  
  440.     return ((WindowPeek)window)->windowKind;
  441.  
  442. }
  443.  
  444.  
  445. //----------------------------------------------------------------------
  446. //
  447. //    GetIsAppWindow - is the window a 'userKind'.
  448. //                 
  449. //
  450. //----------------------------------------------------------------------
  451.  
  452. Boolean GetIsAppWindow(WindowRef window)
  453. {
  454.     return (GetWindKind(window) == kDocKind);
  455.  
  456. }
  457.  
  458.  
  459. //----------------------------------------------------------------------
  460. //
  461. //    GetIsAboutWindow - is the window an about box.
  462. //                 
  463. //
  464. //----------------------------------------------------------------------
  465.  
  466. Boolean GetIsAboutWindow(WindowRef window)
  467. {
  468.     return (GetWindKind(window) == kAboutKind);
  469.     
  470. }
  471.